package com.lwj.springbootvue.config;

import cn.hutool.core.util.ObjectUtil;
import com.lwj.springbootvue.constants.HttpStatus;
import com.lwj.springbootvue.exception.ServiceException;
import com.lwj.springbootvue.mapper.UserMapper;
import com.lwj.springbootvue.model.User;
import com.lwj.springbootvue.model.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;


@Service
public class authenticationDetailsService implements UserDetailsService {


    @Autowired
    private  UserMapper userMapper;


    // 实现 UserDetails 实现自定义登录验证
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 1.查询用户信息
        User user = userMapper.selectOneByUsername(username);
        //System.out.println("登录用户为："+user);
        if(ObjectUtil.isNull(user)){
            throw new ServiceException(HttpStatus.ERROR,"不存在此用户！");
        }
        // 2. 返回UserDetails 对象
        return new LoginUser(user);
    }

}
